define(['ldfw/core', 'text!tpl/user/profile.tpl', 'art','ldfw/ldlibs/dict',
        'comps/ajax','jweixin','comps/session',
        'ldfw/helper/formatHelper','jquery-weui'],
    function (LDFW,tpl,art,dict,ajax,wx,session) {
    let View = LDFW.Container.extend({
        tpl: tpl,
        className: 'panel profile-panel',
        events: {
            'click #avatarCell': 'chooseImage',
            'click [operate="changeGender"]':'changeGender',
            'click [operate="applyAuth"]':'applyAuth',
            'change #avatarCellFile':'fileUploadChange'
        },
        initialize: function () {
            const _this = this
            this.model.set('sexItems',['男','女'])
        },
        compAddHandler:function(comp){
            const _this = this
            comp.on('change',function(selector){
                _this.$el.find('#gender').html(selector.values)
                ajax.post('pfL.3.128',{sex:selector.values})
            })
        },
        loadData:function(){
            const _this = this
            let curr_user = session.getCurrentUser()
            if(curr_user.cust_type == 1){
                ajax.post('pfL.3.107').success(function(resp){
                    let userInfo = _.extend(curr_user,resp.result[0])
                    userInfo.organ_cred_type = dict.getDict('organ_cred_type',userInfo.organ_cred_type)
                    _this.model.set(userInfo)
                    _this.render()
                })
            }else if(curr_user.cust_type == 2){
                ajax.post('pfL.3.106').success(function(resp){
                    let userInfo = _.extend(curr_user,resp.result[0])
                    userInfo.cred_type = dict.getDict('cred_type',userInfo.cred_type)
                    _this.model.set(userInfo)
                    _this.render()
                })
            }
        },
        chooseImage: function () {
            const _this = this
            this.$el.find('#avatarCellFile').trigger('click')
            // wx.chooseImage({
            //     count: 1,
            //     sizeType: ['original', 'compressed'],
            //     sourceType: ['album', 'camera'],
            //     success:function(resp){
            //         var localIds = resp.localIds;
            //         _this.upload(localIds)
            //     }
            // })

        },
        fileUploadChange:function(event){
            const _this = this
            let curr_user = session.getCurrentUser()
            let files = event.target.files;
            let form = new FormData();
            Array.prototype.forEach.call(files,function(file,index){
                form.append('file',file)
                form.append('cust_no',curr_user.cust_no)
                form.append('relation_field',0)
            })
            ajax.upload('fileUpload.do',form).success(function(resp){
                let fileId = resp.result[0]
                _this.$el.find('.profile__img').attr('src','/show_img.do?fileId='+fileId)
                ajax.post('pfL.5.57',{
                    cust_img:fileId
                }).success(function(){
                    $.toast('修改成功', 1000,session.flush())
                })
            })
        },
        // upload:function(localIds){
        //     const _this = this
        //     wx.uploadImage({
        //         localId: localIds[0],
        //         success: function (res) {
        //             let serverId = res.serverId;
        //             ajax.post('uploadWxFile.do',{
        //                 media_id:serverId
        //             }).success(function(resp){
        //                 if(resp.resultCode){
        //                     $.toast('操作失败', 3000);
        //                 }else{
        //                     let fileid = resp.result
        //                     $.toast('提交成功', 3000);
        //                     _this.$el.find('#user-head').attr('src','/show_img.do?fileId='+fileid)
        //                     let currentUser = session.getCurrentUser()
        //                     currentUser.wx_head = '/show_img.do?fileId='+fileid
        //                     session.setCurrentUser(currentUser)
        //                 }
        //             })
        //         },
        //         fail: function (res) {
        //             $.toptip(JSON.stringify(res),1000,'warn');
        //         }
        //     })
        //     wx.error(function (res) {
        //         console.log(res.errMsg);
        //     })
        // },

        changeGender:function(event){
            this.$el.find('[operate="changeGender"]').select('update',{
                input:$(event.currentTarget).find('#gender').text()
            })
        },
        save:function(selector){

        },
        applyAuth:function(event){
            ajax.post('cpliL.3.8',{
                cust_type:this.model.get('cust_type'),
                audit_type:1
            }).success(function(){
                weui.toast("操作成功",1000)
            })
        }
    })
    return new View()
})